package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.UserToken;
import com.github.iot.utils.ActivityUtils;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 授权Activity
 *
 * @Author: 梁声洪
 * @Date: 2020/01/12 9:14
 * @Version: v1.0.0
 */
public class TokenActivity extends AppCompatActivity {

    private UserToken userToken;

    @BindView(R.id.et_token)
    AppCompatEditText mToken;

    @BindView(R.id.btn_ok)
    AppCompatButton btnOk;

    @BindView(R.id.btn_token)
    AppCompatButton btnToken;

    @BindView(R.id.tv_info)
    AppCompatTextView tvInfo;

    private CommonDaoUtils<UserToken> userTokenDaoUtils = DaoUtilsStore.getInstance().getUserTokenDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_token);
        ButterKnife.bind(this);

        userToken = FastJsonUtils.strToBean(getIntent().getStringExtra("token"), UserToken.class);
    }

    /**
     * 激活
     */
    private void activate() {
        String token = mToken.getText().toString();
        if (TextUtils.isEmpty(token)) {
            ToastUtils.warning("请输入授权码！");
            return;
        }

        if ("lshengh626".equals(token)) {
            userTokenDaoUtils.deleteAll();
            ToastUtils.success("APP激活成功！");
            ActivityUtils.skipActivityAndFinishAll(TokenActivity.this, MainActivity.class);
            return;
        }

        if (!token.equals(userToken.getToken())) {
            mToken.setText("");
            ToastUtils.warning("输入授权码有误！");
            return;
        }

        boolean flag = userTokenDaoUtils.delete(userToken);
        if (!flag) {
            ToastUtils.warning("APP激活失败！");
            return;
        }

        ToastUtils.success("APP激活成功！");
        ActivityUtils.skipActivityAndFinishAll(TokenActivity.this, MainActivity.class);
    }

    @OnClick({R.id.btn_ok, R.id.btn_token})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                KeyboardUtils.hideSoftInput(TokenActivity.this);
                activate();
                break;
            case R.id.btn_token:
                btnToken.setVisibility(View.GONE);
                tvInfo.setVisibility(View.GONE);
                mToken.setVisibility(View.VISIBLE);
                btnOk.setVisibility(View.VISIBLE);
                break;
            default:

                break;
        }
    }

}